The following is a list of methods commonly used in Objectdraw, Greenfoot, BlueJ, or possibly all three. Feel free to add, make corrections, or improve the organization of this page.
yourVariable.getX(); returns the x coordinate of an object (Objectdraw)
yourVariable.getY(); returns the y coordinate of an object (Objectdraw)
yourVariable.getWidth(); returns the width of an object (Objectdraw)
yourVariable.getHeight(); returns height of an object
yourVariable.setWidth(amount); sets the width of your object to the given amount
yourVariable.setHeight(amount); sets the height of your object to the given amount
yourRand.nextValue(); assigns the next value for a random number
yourObject.setColor( new Color ( #, #, #)); changes the color of an object to whatever you want using rgb
yourObject.setColor(Color.red);
yourVariable = scan.nextInt();
yourObject.move(x, y); moves an object a set amount x and y units away from initial coordinates
yourObject.moveTo(x, y); moves an object to new coordinates
getRotation();
setLocation(#, #);
setRotation(x);
delay(); (greenfoot)
getRandomNumber(); (greenfoot) works the same way as random generator
boolean isKeyDown(String keyName); (greenfoot)
drawLine(x1, y1, x2, y2); (greenfoot)
Math Functions
Math.sqrt(x) gives square root of value
Math.pow(x, y) this would be x to the power of y
Math.exp(x) e to the power of x
Math.min(x, y) finds the smaller value of the two
Math.max(x, y) finds the larger of the two values
Math.sin(x) finds the sine of x, when x is in radians
Math.cos(x)
Math.tan(x)
Math.toRadians(x) converts degrees to radians
Math.toDegrees(x) //converts radians to degrees
// *************************************************************// Distance.java//// Computes the distance between two points// *************************************************************importjava.util.Scanner;importjava.util.Random;publicclass Distance
{publicstaticvoid main (String[] args){double x1, y1, x2, y2;// coordinatesScanner scan = newScanner(System.in);Random generator = newRandom();System.out.print("Enter the X and Y coordinates of the first point; press enter in between coordinates.");
x1 = scan.nextDouble();
y1 = scan.nextDouble();System.out.print("Enter the X and Y coordinates of the second point: ");
x2 = scan.nextDouble();
y2 = scan.nextDouble();// Compute the distancedouble sumOfSquares = Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2);double distance = Math.sqrt(sumOfSquares);}}
To drag and object in BlueJ with your cursor:
Location lastMouse;//make sure this is under the public class//it creates the location of the cursor so you can//drag the objectpublicvoid onMousePress(Location point){
lastMouse = point;//it makes the point your mouse clicks the//the first point}publicvoid onMouseDrag(Location point){double changeX = point.getX()- lastMouse.getX();double changeY = point.getY()- lastMouse.getY();
object.move(changeX,changeY);
object2.move(changeX,changeY);
lastMouse = point;}//it gets the x and y of the primary point your//cursor was at when you made the location//point and the x and y of the point your cursor//is at now. The changeX and Y are the//differences. The object moves the difference//and the point it is now is stored as the//primary point.
yourVariable.getX(); returns the x coordinate of an object (Objectdraw)
yourVariable.getY(); returns the y coordinate of an object (Objectdraw)
yourVariable.getWidth(); returns the width of an object (Objectdraw)
yourVariable.getHeight(); returns height of an object
yourVariable.setWidth(amount); sets the width of your object to the given amount
yourVariable.setHeight(amount); sets the height of your object to the given amount
yourRand.nextValue(); assigns the next value for a random number
yourObject.setColor( new Color ( #, #, #)); changes the color of an object to whatever you want using rgb
yourObject.setColor(Color.red);
yourVariable = scan.nextInt();
yourObject.move(x, y); moves an object a set amount x and y units away from initial coordinates
yourObject.moveTo(x, y); moves an object to new coordinates
getRotation();
setLocation(#, #);
setRotation(x);
delay(); (greenfoot)
getRandomNumber(); (greenfoot) works the same way as random generator
boolean isKeyDown(String keyName); (greenfoot)
drawLine(x1, y1, x2, y2); (greenfoot)
Math Functions
To drag and object in BlueJ with your cursor: